﻿/*
Main JS for tutorial: "Getting Started with HTML5 Local Databases"
Written by Ben Lister (darkcrimson.com) revised 12 May 2010
Tutorial: http://blog.darkcrimson.com/2010/05/local-databases/

Licensed under the MIT License:
http://www.opensource.org/licenses/mit-license.php
*/

function initDatabase() {
    try {
         if (!window.openDatabase) {
            alert('Local Databases are not supported by your browser. Please use a Webkit browser for this demo');
        } else {
            var shortName = 'dblocalIPickMovies';
            var version = '1.0';
            var displayName = 'DB Local IPickMovies';
            var maxSize = 100000; // in bytes
            dblocalIPickMovies = openDatabase(shortName, version, displayName, maxSize);
            dropTables();
            createTables();
        }
    } catch (e) {
        if (e == 2) {
            // Version mismatch.
            console.log("Invalid database version.");
        } else {
            console.log("Unknown error " + e + ".");
        }
        return;
    }
}



/***
**** CREATE TABLE ** 
***/
function createTables() {
    dblocalIPickMovies.transaction(
        function (transaction) {
            transaction.executeSql('CREATE TABLE IF NOT EXISTS device_settings(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,dkey TEXT INTEGER NOT NULL,cid INTEGER NOT NULL);', [], nullDataHandler, errorHandler);
        }
    );
    //prePopulate();
}


/***
**** INSERT INTO TABLE ** 
***/
function createSettings(dkey,cid) {
    dblocalIPickMovies.transaction(
	    function (transaction) {
	        transaction.executeSql("INSERT INTO device_settings(dkey, cid) VALUES (?, ?)", dkey, cid);
	    }
	);
}

///***
//**** UPDATE TABLE ** 
//***/
//function updateSetting() {
//    dblocalIPickMovies.transaction(
//	    function (transaction) {
//	        if ($('#fname').val() != '') {
//	            var fname = $('#fname').val();
//	        } else {
//	            var fname = 'none';
//	        }

//	        var bg = $('#bg_color').val();
//	        var font = $('#font_selection').val();
//	        var car = $('#fav_car').val();


//	        transaction.executeSql("UPDATE device_settings SET fname=?, bgcolor=?, font=?, favcar=? WHERE id = 1", [fname, bg, font, car]);
//	    }
//	);
//    selectAll();
//}

function setDeviceKey() {
    $('#hiddenDKey').val(device.uuid); 
}

function selectAll() {
    dblocalIPickMovies.transaction(
	    function (transaction) {

    	        transaction.executeSql("SELECT * FROM device_settings;", [], dataSelectHandler, errorHandler);

	    }
	);
}

function dataSelectHandler(transaction, results) {

/*    // Handle the results
    for (var i = 0; i < results.rows.length; i++) {

        var row = results.rows.item(i);

        var newFeature = new Object();

        newFeature.fname = row['fname'];
        newFeature.bgcolor = row['bgcolor'];
        newFeature.font = row['font'];
        newFeature.favcar = row['favcar'];

        $('body').css('background-color', newFeature.bgcolor);
        $('body').css('font-family', newFeature.font);
        $('#content').html('<h4 id="your_car">Your Favorite Car is a ' + newFeature.favcar + '</h4>');

        if (newFeature.fname != 'none') {
            $('#greeting').html('Howdy-ho, ' + newFeature.fname + '!');
            $('#fname').val(newFeature.fname);
        }

        $('select#font_selection').find('option[value=' + newFeature.font + ']').attr('selected', 'selected');
        $('select#bg_color').find('option[value=' + newFeature.bgcolor + ']').attr('selected', 'selected');
        $('select#fav_car').find('option[value=' + newFeature.favcar + ']').attr('selected', 'selected');


    }
*/
}





/***
**** Save 'default' data into DB table **
***/

function saveAll() {
    prePopulate(1);
}


function errorHandler(transaction, error) {
    if (error.code == 1) {
        // DB Table already exists
    } else {
        // Error is a human-readable string.
        console.log('Oops.  Error was ' + error.message + ' (Code ' + error.code + ')');
    }
    return false;
}


function nullDataHandler() {
    console.log("SQL Query Succeeded");
}

/***
**** SELECT DATA **
***/
function selectAll() {
    DEMODB.transaction(
	    function (transaction) {
	        transaction.executeSql("SELECT * FROM device_settings;", [], dataSelectHandler, errorHandler);
	    }
	);
}

/***
**** DELETE DB TABLE ** 
***/
function dropTables() {
    DEMODB.transaction(
	    function (transaction) {
	        transaction.executeSql("DROP TABLE device_settings;", [], nullDataHandler, errorHandler);
	    }
	);
	    console.log("Table 'device_settings' has been dropped.");
    location.reload();
}

	